﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.dz.components;
using com.dz.common;
namespace com.dz.web
{
    public partial class TopList :BasePage
    {
        BookManager m_book_mgr = new BookManager();
        int bookCategoryId = -1;


        const string c_chapter_row_template = "<tr><td class=\"index\">1</td><td class=\"col1\"><a href=\"/category/{0}.aspx\" target=\"_blank\">[{1}]</a></td><td><a class=\"novelname\" title=\"{3}\" href=\"/{2}.aspx\" target=\" class=\"graylink\" title=\"{5}\" href=\"chapter/{4}.aspx\" target=\"_blank\">{5}</a></td><td>{6}</td><td>{7}</td> </tr>";

        const string c_top_container = "<DIV class=bd><DIV class=toplist><DIV class=list12><table cellpadding=\"0\" cellspacing=\"0\">{0}</table></div></div></div>";

        const string c_top_vip_click_template = "<tr><td class=\"book\"><A href=\"/{0}.aspx\" target=_blank>{1}</A></td><td class=\"click\">{2}</td></tr>";

        public string m_title = string.Empty;
        string m_type = string.Empty;

        public int m_page_index = 1;
        int m_page_size = 50;
        int m_total_count = 0;
       

        protected void Page_Load(object sender, EventArgs e)
        {
            m_type = Request["type"];
            if (!int.TryParse(Request["pageIndex"], out m_page_index))
                m_page_index = 1;

            if (!int.TryParse(Request["totalCount"], out m_total_count))
                m_total_count = 0;
            BindTopVipBookList();
            BindList();
        }

        private void BindList()
        {
            try
            {
                int t_total_count = 0;
                DataSet ds = m_book_mgr.GetBookTopList(m_type, out m_title, m_page_index, m_page_size, out t_total_count);
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (t_total_count != 0)
                        m_total_count = t_total_count;
                    rptList.DataSource = ds.Tables[0];
                    rptList.DataBind();
                  
                    divPage.InnerHtml =GetRewritePageHtml(m_page_index, m_page_size, m_total_count, m_type);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定列表失败" + ex.ToString());
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Page.Title = "书库,言情,武侠,都市,历史,军事,科幻,女频,青春,校园,奇幻,最新章节," + AppConfig.SiteName;
            if (this.Master is Default)
            {
                Default master = this.Master as Default;
                master.SetPageMetaKeyword(m_title + ",最新,最新章节,免费阅读" + ",免费下载");
                master.SetPageMetaDescription(string.Format("{1}提供{0}全文免费阅读、{0}最新章节和列表,免费{0}电子书、{0}TXT下载。看{0}最新章节就到{0}免费小说{1}。",
                     m_title, AppConfig.SiteName));
            }

        }

        /// <summary>
        /// 获取分页HTML
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        protected string GetRewritePageHtml(int pageIndex, int pageSize, int count, string type)
        {

            StringBuilder sb = new StringBuilder();
            StringBuilder sbhead = new StringBuilder();
            sbhead.Append("<div id=\"pager_blockdd\">");
            sbhead.Append("<div id=\"pager\">");
            int pageCount = (int)Math.Ceiling((double)count / pageSize);
            int start = ((pageIndex - 1) / 10) * 10 + 1;
            sbhead.AppendFormat("<a href=\"{0}\" title=\"上一页\">上一页</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type, count, (pageIndex - 1)));
            for (int i = start; i <= pageCount && i < start + 10; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<span title=\"当前第{0}页\" class=\"current\">{1}</span>", i, i);
                else
                    sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页\">{2}</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type,count, i), i, i);
            }
            if (start > 1)
            {
                sbhead.Append(string.Format("<a href=\"{0}\" title=\"首页\">{1}</a>", string.Format("/topList/{0}/{1}.aspx", type, 1), 1));
                sbhead.Append(string.Format("<a href=\"{0}\" title=\"第{1}页\">...</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type, count, (start - 1)), (start - 1)));
               
            }
            if (start + 10 <= pageCount)
            {
                sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页\">...</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type, count,(start + 10)), start + 10);
                sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页(末页)\">{2}</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type,count, pageCount), pageCount, pageCount);
            }
            sb.AppendFormat("<a href=\"{0}\" title=\"下一页\">下一页</a>", string.Format("/topList/{0}/{1}/{2}.aspx", type, count,(pageIndex + 1)));
            sb.Append("</div></div>");
            return sbhead.ToString()+ sb.ToString();
        }


        /// <summary>
        /// 绑定VIP榜单数据列表
        /// </summary>
        private void BindTopVipBookList()
        {


            try
            {
                DataSet ds = m_book_mgr.GetBookIntorTopBookList(bookCategoryId);
                StringBuilder sb = new StringBuilder();
                if (ds != null && ds.Tables.Count > 0)
                {
                    int t_count = ds.Tables[0].Rows.Count;
                    //VIP点击
                    if (t_count >= 0)
                    {
                        for (int index = 0; index < t_count; index++)
                        {
                            if (index == 12)
                                break;
                            sb.AppendFormat(c_top_vip_click_template, ds.Tables[0].Rows[index]["bookId"], SystemUtil.GetGBString(ds.Tables[0].Rows[index]["bookName"].ToString(), 14), ds.Tables[0].Rows[index]["click"]);
                        }
                        lblVipClick.Text =string.Format(c_top_container, sb.ToString());
                        sb = new StringBuilder();
                    }
                    //VIP最新更新
                    if (t_count > 12)
                    {
                        for (int index = 12; index < t_count; index++)
                        {
                            if (index == 24)
                                break;
                            sb.AppendFormat(c_top_vip_click_template, ds.Tables[0].Rows[index]["bookId"], SystemUtil.GetGBString(ds.Tables[0].Rows[index]["bookName"].ToString(), 14), Convert.ToDateTime(ds.Tables[0].Rows[index]["lastupdatetime"].ToString()).ToString("dd HH:mm"));

                        }
                        lblVipUpdateTime.Text = string.Format(c_top_container, sb.ToString());
                        sb = new StringBuilder();
                    }
                    //最新入库
                    if (t_count > 24)
                    {
                        for (int index = 24; index < t_count; index++)
                        {
                            if (index == 36)
                                break;
                            sb.AppendFormat(c_top_vip_click_template, ds.Tables[0].Rows[index]["bookId"], SystemUtil.GetGBString(ds.Tables[0].Rows[index]["bookName"].ToString(), 14), Convert.ToDateTime(ds.Tables[0].Rows[index]["getTime"].ToString()).ToString("dd HH:mm"));

                        }

                        lblGetTime.Text = string.Format(c_top_container, sb.ToString());
                        sb = new StringBuilder();
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定VIP榜单书籍失败" + ex.ToString());
            }
        }
    }

    
}
